﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vant.Properties;
using System.Windows;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class InputDesigner : InputCellTypeBaseDesigner<Input>
    {
        public override void InitDefaultPropertyValues(IBuilderContext context)
        {
            base.InitDefaultPropertyValues(context);
            this.CellType.placeholder = Resources.Input_PlaceEnter;
        }

        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            InputDrawingObject inputDrawing = base.GetDrawingControl(cellInfo, drawingHelper) as InputDrawingObject;

            if (CellType.type == "textarea")
            {
                inputDrawing.ViewModel.leftIcon = null;
                inputDrawing.ViewModel.rightIcon = null;
            }

            return inputDrawing;
        }
    }

    [Designer("Vant.CellTypes.InputDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Input.png")]
    [OrderWeight((int)Utils.OrderWeight.Input)]
    public class Input : InputCellTypeBase
    {
        public override List<Command> CommandList { get => base.CommandList; set => base.CommandList = value; }
        public override DataValidationLink DataValidationLink { get => base.DataValidationLink; set => base.DataValidationLink = value; }
        public override List<UIPermission> UIPermissions { get => base.UIPermissions; set => base.UIPermissions = value; }
        public override object DefaultValue { get => base.DefaultValue; set => base.DefaultValue = value; }

        [SRDisplayName(nameof(Resources.Input_Type))]
        [SRRadioGroupProperty(ValueList = "text|textarea|password",
            DisplayList = nameof(Resources.Input_RadioGroupProperty_DisplayList))]
        public string type { get; set; } = "text";

        [SRDisplayName(nameof(Resources.Input_KeyboardType))]
        [SRComboProperty(ValueList = "text|tel|digit|number|date|datetime-local|time|email|url",
           DisplayList = nameof(Resources.Input_KeyboardType_ComboProperty_DiaplayList))]
        public string keyboardType { get; set; } = "text";

        [SRDisplayName(nameof(Resources.Input_Enterkeyhint))]
        [SRComboProperty(ValueList = "enter|done|go|next|previous|search|send", DisplayList = nameof(Resources.Input_Enterkeyhint_DisplayList))]
        public string enterkeyhint { get; set; } = "enter";

        [SRDisplayName(nameof(Resources.Input_Maxlength))]
        [FormulaProperty]
        public object maxlength { get; set; }

        [SRDisplayName(nameof(Resources.Input_ShowWordLimit))]
        public bool showWordLimit { get; set; }

        public override string placeholder { get => base.placeholder; set => base.placeholder = value; }
        public override ImageValue leftIcon { get => base.leftIcon; set => base.leftIcon = value; }
        public override ImageValue rightIcon { get => base.rightIcon; set => base.rightIcon = value; }
        [SRCategoryHeader(nameof(Properties.Resources.AdjustResize))]
        [SRDisplayName(nameof(Properties.Resources.AutosizeHeight))]
        [BoolProperty(IndentLevel = 1)]
        [DefaultValue(false)]
        public bool autosize { get; set; } = false;

        [IntProperty]
        [SRDisplayName(nameof(Properties.Resources.MinHeight))]
        [DefaultValue(50)]
        public int minHeight { get; set; } = 50;

        [IntProperty]
        [SRDisplayName(nameof(Properties.Resources.MaxHeight))]
        [DefaultValue(100)]
        public int maxHeight { get; set; } = 100;
        public override bool border { get => base.border; set => base.border = value; }
        public override bool clearable { get => base.clearable; set => base.clearable = value; }
        public override bool ReadOnly { get => base.ReadOnly; set => base.ReadOnly = value; }
        public override bool IsDisabled { get => base.IsDisabled; set => base.IsDisabled = value; }

        public override string ToString()
        {
            return Properties.Resources.Input;
        }
        [SRDisplayName(nameof(Resources.Input_Focus))]
        [RunTimeMethod]
        [SRDescription(nameof(Resources.Input_Focus_Description))]
        public void Focus()
        {
        }

        [SRDisplayName(nameof(Resources.Input_Select))]
        [SRDescription(nameof(Resources.Input_Select_Descrption))]
        [RunTimeMethod]
        public void Select()
        {
        }
        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(showWordLimit))
            {
                return !string.IsNullOrEmpty(maxlength?.ToString());
            }
            if (propertyName == nameof(keyboardType))
            {
                return type == "text";
            }
            if (propertyName == nameof(autosize))
            {
                return type == "textarea";
            }
            if (propertyName == nameof(minHeight) || propertyName == nameof(maxHeight))
            {
                return type == "textarea" && autosize;
            }
            if (
                propertyName == nameof(leftIcon) ||
                propertyName == nameof(rightIcon) ||
                propertyName == nameof(clearable)
                )
            {
                return type != "textarea";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }
    }
}
